﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LaughingDog
{
    public static class Screen
    {
        private static Color mBackColour = Color.CornflowerBlue;

        /// <summary>
        /// The colour used when clearing the GraphicsDevice.
        /// </summary>
        public static Color BackgroundColour { get { return mBackColour; } set { mBackColour = value; } }

        /// <summary>
        /// Gets or sets the resolution of the game window.
        /// </summary>
        public static Vector2 Size { get { return GetScreenSize(); } set { SetScreenSize(value); } }

        /// <summary>
        /// Gets or sets the horizontal resolution of the game window.
        /// </summary>
        public static Int32 Width { get { return (Int32)GetScreenSize().X; } set { SetScreenSize(new Vector2(value, Height)); } }

        /// <summary>
        /// Gets or sets the vertical resolution of the game window.
        /// </summary>
        public static Int32 Height { get { return (Int32)GetScreenSize().Y; } set { SetScreenSize(new Vector2(Width, value)); } }

        /// <summary>
        /// Gets or sets whether the game is running in fullscreen mode.
        /// </summary>
        public static Boolean IsFullScreen { get { return Engine.GraphicsDeviceManager.IsFullScreen; } set { Engine.GraphicsDeviceManager.IsFullScreen = value; Engine.GraphicsDeviceManager.ApplyChanges(); } }

        /// <summary>
        /// Gets the centre point of the screen.
        /// </summary>
        public static Vector2 Centre { get { return Screen.Size / 2; } }

        /// <summary>
        /// Gets the centre bottom point of the screen.
        /// </summary>
        public static Vector2 CentreBottom { get { return new Vector2(Centre.X, Height); } }

        /// <summary>
        /// Gets the centre top point of the screen.
        /// </summary>
        public static Vector2 CentreTop { get { return new Vector2(Centre.X, 0); } }

        /// <summary>
        /// Gets the centre top point of the screen.
        /// </summary>
        public static Vector2 CentreLeft { get { return new Vector2(0, Height / 2); } }

        /// <summary>
        /// Gets the centre right point of the screen.
        /// </summary>
        public static Vector2 CentreRight { get { return new Vector2(Width, Height / 2); } }

        /// <summary>
        /// Gets or sets a value that indicates whether to sync to the vertical retrace (vsync) when presenting the back buffer.
        /// </summary>
        public static Boolean SynchroniseWithVerticalRetrace { get { return Engine.GraphicsDeviceManager.SynchronizeWithVerticalRetrace; } set { Engine.GraphicsDeviceManager.SynchronizeWithVerticalRetrace = value; } }

        private static Vector2 GetScreenSize()
        {
            return new Vector2(Engine.GraphicsDeviceManager.PreferredBackBufferWidth, Engine.GraphicsDeviceManager.PreferredBackBufferHeight);
        }

        private static void SetScreenSize(Vector2 size)
        {            
            Engine.GraphicsDeviceManager.PreferredBackBufferWidth = (Int32)size.X;
            Engine.GraphicsDeviceManager.PreferredBackBufferHeight = (Int32)size.Y;
            Engine.GraphicsDeviceManager.ApplyChanges();
        }

        /// <summary>
        /// Toggles between fullscreen and windowed mode.
        /// </summary>
        public static void ToggleFullScreen()
        {
            Engine.GraphicsDeviceManager.ToggleFullScreen();            
        }
    }
}
